﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aspose.Cells;
using CodeGenerator.Model;
using RazorEngine;

namespace CodeGenerator
{
    public class Class1
    {
        public void GenerateByRazor()
        {
            var template = File.ReadAllText(@"D:\Project\lots\CodeGenerator\template\OrderPlaced.html");


            var viewModel = new OrderViewModel
            {
               Name = "kkdkd",
               Address = "kdkdkrer93939",
            };
            var body = Razor.Parse(template, viewModel);
            Console.WriteLine(body);
        }


        public void GenerateAccount()
        {
            var statementTemplatePath = @"D:\Project\lots\CodeGenerator\template\accounttemplate.xlsx";
            //var fstream = new FileStream(statementTemplatePath, FileMode.Open);

            var workbook = new Workbook(statementTemplatePath);


            var checkoutDetail = workbook.Worksheets["sheet1"];
            var cells = checkoutDetail.Cells;

            var rows = cells.Rows.Count;
            var columns = 3;

            var matouusertempate = "declare @uid int\n"
                                   + " insert into Ymt_UserIds (dAddTime) values (GETDATE())\n"
                                   + " set @uid=@@identity\n" +
                                   " insert into Ymt_Users (iuserid,sLoginId,sPassword,sLoginEmail,sNickName,iType,iAction,dAddTime) \n" +
                                   " values (@uid,'{1}','{2}','{4}','{1}',10,1,GETDATE())\n";



            var template = "update merchants set companyname = '{2}', contactname='{3}',MerchantStreet='{4}'," +
                           "MerchantCity='{5}'," +
                           "zipcode='{7}'," +
                           "phone='{8}'," +
                           "TradingPassword='{10}' " +
                           "where name='{1}'\n";

            var sb = new StringBuilder();
            for (var i = 1; i < rows; i++)
            {
                var newstr = template;
                for (var j = 0; j < columns; j++)
                {
                    var obj = cells[i, j].Value;
                    var cellValue = obj == null ? string.Empty : obj.ToString();

                    var key = "{" + (j + 1) + "}";
                    newstr = newstr.Replace(key, cellValue);

                }
                sb.Append(newstr);
            }


            // fstream.Close();
            Console.WriteLine(sb.ToString());
        }

        public void Generate()
        {
            var statementTemplatePath = @"D:\Project\lots\CodeGenerator\template\exceltemplate.xlsx";
            //var fstream = new FileStream(statementTemplatePath, FileMode.Open);

            var workbook = new Workbook(statementTemplatePath);


            var checkoutDetail = workbook.Worksheets["sheet1"];
            var cells = checkoutDetail.Cells;

            var rows = cells.Rows.Count;
            var columns = 10;

            var template = "update merchants set MerchantCompanyFullName = '{2}', contactname='{3}',MerchantStreet='{4}'," +
                           "MerchantCity='{5}'," +
                           "zipcode='{7}'," +
                           "phone='{8}'," +
                           "TradingPassword='{10}' " +
                           "where name='{1}'\n";

            var sb = new StringBuilder();
            for (var i = 1; i < rows; i++)
            {
                var newstr = template;
                for (var j = 0; j < columns; j++)
                {
                    var obj= cells[i, j].Value;
                    var cellValue = obj == null? string.Empty: obj.ToString();

                    var key = "{" + (j + 1)+"}";
                    newstr = newstr.Replace(key, cellValue);

                }
                    sb.Append(newstr);
            }

            
           // fstream.Close();
            Console.WriteLine(sb.ToString());

        }
    }
}
